﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Torch_Services
{
    public class DesertService : IDesertService
    {
        private IDesertDal _desert;
        private IMapper _mapper;
        public DesertService(IDesertDal desert, IMapper mapper)
        {
            _desert = desert;
            _mapper = mapper;
        }

        public async Task<int> AddDesertAsync(string description, decimal desertPrice)
        {
            return await _desert.AddAsync(new Desert 
            {
                Description = description,
                DesertPrice = desertPrice
            });
        }

        public async Task<int> DeleteDesertAsync(int id)
        {
            var data = await _desert.QueryAsync(id);
            return data == null ? -1 : await _desert.DeleteAsync(data);
        }

        public async Task<int> EditDesertAsync(int id, string description, decimal desertPrice)
        {
            var data = await _desert.QueryAsync(id);
            if (data == null)
                return -1;
            data.Description = description;
            data.DesertPrice = desertPrice;
            return await _desert.EditAsync(data);
        }

        public async Task<List<DesertDto>> QueryDesertAllAsync()
        {
            var data = await _desert.QueryAsync();
            return _mapper.Map<List<Desert>,List<DesertDto>>(data);
        }

        public async Task<DesertDto> QueryDesertByIdAsync(int id)
        {
            var data = await _desert.QueryAsync(id);
            return _mapper.Map<Desert,DesertDto>(data);
        }

        public async Task<List<DesertDto>> QueryDesertByNameAsync(string description)
        {
            var data = await _desert.QueryAsync(dt => dt.Description.Contains(description));
            return _mapper.Map<List<Desert>, List<DesertDto>>(data);
        }
    }
}
